﻿//wrapper
var chirper = {};
chirper.base = "9er";
chirper.cage_Profile = [];

// constructor for profiles
chirper.Profile = function (image, name, bio) {
    this.image = image;
    this.name = name;
    this.bio = bio;
};
chirper.Profile.prototype.isUpdated = false;
// constructor for friends
chirper.Friend = function (name, domain) {
    this.name = name;
    this.domain = domain;
};
chirper.Friend.prototype.isFriend = false;

chirper.urlHelper = function (base) {
    var url = "https://" + base + ".firebaseio.com/"
    for (var i = 1; i < arguments.length; i++) {
        url += arguments[i] + "/"
    }
    return url + ".json";
}
//draw table
chirper.drawTable_Profile = function (index) {
    var h = chirper.cage_Profile[index];
    document.getElementById("imageProfile").innerHTML = h.image;
    document.getElementById("nameProfile").innerHTML = h.name;
    document.getElementById("bioProfile").innerHTML = h.bio;
};


chirper.addMessage_Profile = function () {
    "use strict"
    var image, name, bio;
    image = document.getElementById("image");
    name = document.getElementById("name");
    bio = document.getElementById("bio");
    var s = new chirper.Profile(image.value, name.value, bio.value)
    chirper.cage_Profile.push(s);
    chirper.ajax("POST", chirper.urlHelper(chirper.base, "profile"), s, function () {
        chirper.read_Profile();
        document.getElementById("startProfile").onclick = function () { chirper.startModalSave_Profile(); };
        $("#startModal").modal();
    });

};

chirper.startModalSave_Profile = function () {
    $("#startModal").modal("hide");
    chirper.drawTable_Profile();
};

chirper.read_Profile = function () {
    chirper.cage_Profile = [];
    chirper.ajax("GET", chirper.urlHelper(chirper.base, "profile"), null, function (data) {
        for (var i in data) {
            var chirp = new chirper.Profile(data[i].image, data[i].name, data[i].bio); // parameters
            chirp.key = i;
            chirper.cage_Profile.push(chirp);
        }
        chirper.drawTable_Profile();
    })
};

//Delete Message
chirper.delete_Profile = function (index) {
    chirper.ajax("DELETE", chirper.urlHelper(chirper.base, "profile", chirper.cage_Profile[index].key), null, function () {
        chirper.read_Profile();
    });
    setTimeout(function () { chirper.cage_Profile.splice(index, 1); }, 2000);
    chirper.drawTable_Profile();
}
//Show Edit
chirper.showEdit_Chirp = function (index) {
    var chirp = chirper.cage_Profile[index];
    //document.getElementById("editTitle").innerHTML = "Message Edit" + chirp.name;
    document.getElementById("editImage").value = chirp.image;
    document.getElementById("editName").value = chirp.name;
    document.getElementById("editBio").value = chirp.bio;
    document.getElementById("isUpdated").value = chirp.isFriend.toString();
    document.getElementById("saveChangesBtn").onclick = function () { chirper.saveEdit_Profile(index); };
    $("#editModal").modal();

};
// Save Edit
chirper.saveEdit_Profile = function (index) {
    var chirp = chirper.cage_Profile[index];
    chirp.image = document.getElementById("editImage").value;
    chirp.name = document.getElementById("editName").value;
    chirp.bio = document.getElementById("editBio").value;
    if (document.getElementById("isUpdated").value === "true") {
        chirp.isUpdated = true;
    }
    else {
        delete chirp.isFriend;
    }
    var s = new chirper.Profile(chirp.name, chirp.bio);
    $("#editModal").modal("hide");
    chirper.drawTable_Profile();
    chirper.ajax("PATCH", chirper.urlHelper(chirper.base, "profile", chirper.cage_Profile[index].key), s, function () {
        chirper.read_Profile();
    });
};

/**************** AJAX *****************/
chirper.ajax = function (method, url, data, success, error) {
    var request = new XMLHttpRequest();
    request.open(method, url);
    request.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            success(JSON.parse(this.response));
        } else {
            console.log("Error on " + method);
            error();
        }
    };
    request.onerror = function () {
        console.log("Communication error");
        error();
    }
    if (data) {
        request.send(JSON.stringify(data));
    } else {
        request.send();
    }
};
/*****************************************/
//Clear All
chirper.clearAll_Chirp = function () {
    chirper.cage_Profile = [];
    chirper.drawTable_Profile();
}
//Seeder
chirper.read_Profile();
$("#startModal").modal();